package com.bw.common.mvvm.view;

import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewbinding.ViewBinding;

import com.blankj.utilcode.util.BarUtils;
import com.bw.common.bar.StatusBar;
import com.bw.common.entity.LiveDataEntity;
import com.bw.common.mvvm.viewmodel.BaseViewModel;
import com.umeng.analytics.MobclickAgent;

import java.lang.ref.WeakReference;
import java.lang.reflect.ParameterizedType;

public abstract class BaseActivity<V extends ViewBinding,VM extends BaseViewModel> extends AppCompatActivity implements IView {
    protected LayoutInflater inflater = null;
    private LiveDataActivity liveDataActivity = null;
    protected V v;
    protected VM vm;

    public abstract String getClassNames();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        liveDataActivity = new LiveDataActivity(this);
        inflater = LayoutInflater.from(this);
        v = (V) initBinding();

        StatusBar.fitSystemBar(this);

        setContentView(bindLayout());
        initViewModel();
        getLifecycle().addObserver(vm);
        vm.getLiveDataViewModel().getUiData().observe(this,liveDataActivity);
        initView();
        initData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("" + getClassNames());
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(""+getClassNames());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        vm.getLiveDataViewModel().getUiData().removeObserver(liveDataActivity);
        getLifecycle().removeObserver(vm);
    }

    @Override
    public void initViewModel() {
        ParameterizedType type = (ParameterizedType) getClass().getGenericSuperclass();
        Class clazz = (Class) type.getActualTypeArguments()[1];
        vm = (VM) new ViewModelProvider(this,new ViewModelProvider.NewInstanceFactory())
                .get(clazz);
    }

    public static class LiveDataActivity implements Observer<LiveDataEntity> {
        private WeakReference<BaseActivity> weakReference;

        public LiveDataActivity(BaseActivity activity) {
            weakReference = new WeakReference<>(activity);
        }

        @Override
        public void onChanged(LiveDataEntity liveDataEntity) {
            if (weakReference.get() == null||liveDataEntity == null || liveDataEntity.action == null){
                return;
            }
            weakReference.get().handleMessage(liveDataEntity);
        }
    }

    protected abstract void handleMessage(LiveDataEntity entity);
}
